﻿using Ewell.BizComponent.Service.Model.Insur.Response;
using Ewell.BizComponent.Service.Model.InterProxy;
using Ewell.BizComponent.Service.Model.Request;
using Ewell.BizComponent.Service.Model.Response;
using Ewell.BizComponent.UI.Charge.Constant;
using Ewell.BizComponent.UI.Core.VievModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Ewell.BizComponent.UI.Charge.ViewModel
{
    /// <summary>
    /// 收费主类
    /// </summary>
    public class ChargeVm:ViewModelBase
    {
        /// <summary>
        /// 预算状态
        /// </summary>
        private SettlementStatus _preSettlementState;
        public SettlementStatus preSettlementState
        {
            get { return _preSettlementState; }
            set
            {
                if(_preSettlementState != value )
                {
                    _preSettlementState = value;
                    OnPropertyChanged(nameof(preSettlementState));
                }
            }
        }
        /// <summary>
        /// 结算状态
        /// </summary>
        private SettlementStatus _settlementState;
        public SettlementStatus settlementState
        {
            get { return _settlementState; }
            set
            {
                if (_settlementState != value)
                {
                    _settlementState = value;
                    OnPropertyChanged(nameof(settlementState));
                }
            }
        }
        /// <summary>
        /// 收费预算请求
        /// </summary>
        public OutPreSettlementRequest outPreSettlementRequest { get; set; }
        /// <summary>
        /// 预算主类
        /// </summary>
        private PreSettlementVm _preSettlementVm;
        public PreSettlementVm preSettlementVm
        {
            get => this._preSettlementVm;
            set
            {
                if(_preSettlementVm != value )
                {
                    _preSettlementVm = value;
                    OnPropertyChanged(nameof(_preSettlementVm));
                }
            }
        }
        /// <summary>
        /// 结算主类
        /// </summary>
        private SettlementVm _settlementVm;
        public SettlementVm settlementVm
        {
            get => _settlementVm; set
            {
                if (_settlementVm != value)
                {
                    _settlementVm = value;
                    OnPropertyChanged(nameof(settlementVm));
                }
            }
        }
        /// <summary>
        /// 患者信息
        /// </summary>
        private PatientVm _patientVm;
        public PatientVm patientVm
        {
            get => _patientVm; set
            {
                if (value != _patientVm)
                {
                    _patientVm = value;
                    OnPropertyChanged(nameof(patientVm));
                }
            }
        }

        /// <summary>
        /// 预算显示
        /// </summary>
        private Visibility _preSettlementViewVisib=Visibility.Collapsed;
        public Visibility preSettlementViewVisib
        {
            get => _preSettlementViewVisib;
            set
            {
                if(_preSettlementViewVisib != value)
                {
                    _preSettlementViewVisib = value;
                    OnPropertyChanged(nameof(preSettlementViewVisib));
                }
            }
        }
        /// <summary>
        /// 结算显示
        /// </summary>
        private Visibility _settlementViewVisib = Visibility.Collapsed;
        public Visibility settlementViewVisib
        {
            get => _settlementViewVisib;
            set
            {
                if(_settlementViewVisib != value)
                {
                    _settlementViewVisib = value;
                    OnPropertyChanged(nameof(settlementViewVisib));
                }
            }
        }

        public Window localWindow { get; set; }

        /// <summary>
        /// 下一步
        /// </summary>
        public ICommand MoveNext => new RelayCommand((object param) =>
        {
            if (preSettlementVm.outPreSettlementResponse == null)
                preSettlementViewVisib = Visibility.Visible;
            if(preSettlementViewVisib== Visibility.Visible)
            {
                if ((preSettlementVm.InsurFlag && preSettlementVm.preOutPayResponse == null)
            || (!preSettlementVm.InsurFlag && preSettlementVm.outPreSettlementResponse == null))
                    preSettlementVm.PreSettlement.Execute(this);
                if ((preSettlementVm.InsurFlag && preSettlementVm.preOutPayResponse != null)
                || (!preSettlementVm.InsurFlag && preSettlementVm.outPreSettlementResponse != null))
                {
                    preSettlementViewVisib = Visibility.Collapsed;
                    preSettlementState = SettlementStatus.Finash;
                    settlementVm = new SettlementVm()
                    {
                        patient = preSettlementVm.patient,
                        selectedPatientType = preSettlementVm.selectedPatientType,
                        chargingInfo = new ChargingInfoVm()
                        {
                            settlementId = preSettlementVm.outPreSettlementResponse.settlementId,
                            settlementTypeCode = preSettlementVm.outPreSettlementResponse.settlementTypeCode,
                            amount = preSettlementVm.outPreSettlementResponse.amount,
                            discountAmount = preSettlementVm.outPreSettlementResponse.discountAmount,
                            encounterId = preSettlementVm.outPreSettlementRequest.encounter.encounterId,
                            insurAmount = preSettlementVm.preOutPayResponse?.insurReimAmount,
                        },
                        cashierVm = new CashierVm()
                        {
                            selectedPatientType = preSettlementVm.selectedPatientType,
                            patientVm = preSettlementVm.patient,
                        }
                    };
                    settlementVm.cashierVm.chargingInfo = settlementVm.chargingInfo;
                    settlementVm.cashierVm.Init.Execute(settlementVm);
                    settlementViewVisib = Visibility.Visible;
                    settlementState = SettlementStatus.Running;
                    settlementVm.Settlement.Execute(settlementVm);
                    return;
                }
            }

            if(settlementViewVisib == Visibility.Visible)
            {
                settlementVm.Settlement.Execute(settlementVm);
            }
            if (settlementVm!=null&&settlementVm.outChargeResponse != null)
                localWindow?.Close();
        });
        /// <summary>
        /// 取消
        /// </summary>
        public ICommand Cancel => new RelayCommand((object param) =>
        {
            if(settlementViewVisib==Visibility.Visible)
            {
                settlementVm.CancelSettlement.Execute(this);
                preSettlementVm.CancelPreSettlement.Execute(this);
                localWindow?.Close();
            }
            if (preSettlementViewVisib == Visibility.Visible)
            {
                preSettlementVm.CancelPreSettlement.Execute(this);
                localWindow?.Close();
            }
        });
    }
}
